package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Zykzsxsbzp;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.ZykzsxsbzpService;
import com.bci.pwtz.mysql.dao.ZykzsxsbzpMapper;
@Service
public class ZykzsxsbzpServiceImpl implements ZykzsxsbzpService{
	
	private ZykzsxsbzpMapper objMapper;
	private CompanyService companyService;
	private BusinessRefService businessRefService;
	private static final Logger _logger = Logger.getLogger(ZykzsxsbzpServiceImpl.class);

	/**
	 *    
	 * @return
	 */
	public ZykzsxsbzpMapper getObjMapper() {
			return objMapper;
		}
	/**
	 * 
	 * @param objMapper
	 */
	public void setObjMapper(ZykzsxsbzpMapper objMapper) {
			this.objMapper = objMapper;
		}
	
	public CompanyService getCompanyService()
    {
        return companyService;
    }
    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }
    
    public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }
    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }
    /**
	 * 添加扩展属性设备造件
	 * @param zykzsxsbzp 扩展属性设备造件对象
	 * @return 扩展属性设备造件对象
	 * @throws PwtzDatabaseException
	 */
	public Zykzsxsbzp add(Zykzsxsbzp zykzsxsbzp) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled())
		{
			_logger.debug("add:zykzsxsbzp"+zykzsxsbzp.toString());
		}
		if(zykzsxsbzp==null)
		{
			if(_logger.isDebugEnabled())
			{
				_logger.debug("add,zykzsxsbzp is null");
			}
			return null;
		}else{
			if(zykzsxsbzp.getId()==null){
			    UUID uuid=UUID.randomUUID();
	            zykzsxsbzp.setDeviceid(uuid.toString());
			}
			zykzsxsbzp.setStatus(1);
			zykzsxsbzp.setCreatedate(new Date());
			zykzsxsbzp.setLastmodifiedtime(new Date());
		}
		try{
			objMapper.insertSelective(zykzsxsbzp);
		}catch(DataAccessException _e){
			_logger.error(ExceptionUtils.getStackTrace(_e));
			throw new PwtzDatabaseException();
		}
		return zykzsxsbzp;
	}
	/**
	 * 根据id查询扩展属性设备造件
	 * @param kzsxsbzpId 造件id
	 * @return 扩展属性设备造件
	 * @throws PwtzDatabaseException
	 */
	public Zykzsxsbzp load(String kzsxsbzpId) throws PwtzDatabaseException {
		if(_logger.isInfoEnabled())
		{
			_logger.info("load:kzsxsbzpId="+kzsxsbzpId);
		}
		Zykzsxsbzp zykzsxsbzp=null;
		try{
			zykzsxsbzp=objMapper.selectByPrimaryKey(kzsxsbzpId);
		}catch(DataAccessException _e){
			_logger.error(ExceptionUtils.getStackTrace(_e));
			throw new PwtzDatabaseException();
		}
		if(zykzsxsbzp!=null)
		{
			_logger.debug("load:result zykzsxsbzp="+zykzsxsbzp.toString());
		}
		return zykzsxsbzp;
	}

	/**
	 * 根据id删除扩展属性设备造件
	 * @param kzsxsbzpId 造件id
	 * @return 删除记录行数
	 * @throws PwtzDatabaseException
	 * @throws CanNotDeleteException 
	 */
	public int delete(String kzsxsbzpId) throws PwtzDatabaseException, CanNotDeleteException {
		int result=0;
		if(_logger.isDebugEnabled())
		{
			_logger.debug("delete:kzsxsbzpId="+kzsxsbzpId);
		}
		try{
            boolean bl = businessRefService.canDelete("T_ZYKZSXSBZP", kzsxsbzpId);
            if(!bl){
                throw new CanNotDeleteException();
            }
			result=objMapper.deleteById(kzsxsbzpId);
		}catch(DataAccessException _e){
			_logger.error(ExceptionUtils.getStackTrace(_e));
			throw new PwtzDatabaseException();
		}
		return result;
	}

	/**
	 * 修改扩展属性设备造件
	 * @param zykzsxsbzp 扩展属性设备造件对象
	 * @return 修改记录行数
	 * @throws PwtzDatabaseException
	 */
	public int update(Zykzsxsbzp zykzsxsbzp) throws PwtzDatabaseException {
		int result=0;
		if(_logger.isDebugEnabled())
		{
			_logger.debug("update:zykzsxsbzp="+zykzsxsbzp.toString());
		}
		if(zykzsxsbzp==null)
		{
			if(_logger.isDebugEnabled()){
				_logger.debug("update,zykzsxsbzp is null");
			}
			return result;
		}else{
		    zykzsxsbzp.setLastmodifiedtime(new Date());
		}
		try{
			result=objMapper.updateByPrimaryKeySelective(zykzsxsbzp);
		}catch(DataAccessException _e){
			_logger.error(ExceptionUtils.getStackTrace(_e));
			throw new PwtzDatabaseException();
		}
		return result;
	}
	
	/**
	 * 
	 * @param name 造件名称
	 * @param companyid 公司名称
	 * @param beginTime 起始时间
	 * @param endTime 结束时间
	 * @param pageSize 每页记录数
	 * @param pageNo 当前页数
	 * @return 当前页的记录集合
	 * @throws PwtzDatabaseException
	 */
	public PaginationList<Zykzsxsbzp> findZykzsxsbzp(Long companyid,int portalType,String[] medLineCodes,
			String beginTime, String endTime, int pageSize, int pageNo,String currentDeviceId)
			throws PwtzDatabaseException {
		PaginationList<Zykzsxsbzp> paginationList=new PaginationList<Zykzsxsbzp>();
		if(_logger.isInfoEnabled())
		{
		    _logger.info("findKzsxsbzp:|companyid="+companyid+"|portalType="+portalType+"|medLineCodes="+medLineCodes+"|beginTime"+beginTime+"|endTime="+endTime+"|pageSize="+pageSize+"|pageNo="+pageNo);
		}
		List<Zykzsxsbzp> list=null;
		int beginIndex=pageSize*pageNo-pageSize;
		String companyidStr = "";
        String[] companyids=null;
        if(portalType==1){
            companyidStr = companyService.findChildrensId(companyid,null);
            if(companyidStr!=null && companyidStr.length()>0){
                companyidStr= companyidStr.substring(0,companyidStr.length()-1);
                companyids =companyidStr.split(",");
            }
        }
		try{
			list=objMapper.selectDeviceList(companyid,companyids,portalType,medLineCodes, beginTime, endTime, beginIndex, pageSize,currentDeviceId);
		}catch(DataAccessException _e){
			_logger.error(ExceptionUtils.getStackTrace(_e));
			throw new PwtzDatabaseException();
		}
		int totalCount=0;
		int totalPage=1;
		if(pageSize!=0)
		{
			try{
				totalCount=objMapper.selectDeviceCount(companyid,companyids,portalType,medLineCodes, beginTime, endTime);
			}catch(DataAccessException _e){
				_logger.error(ExceptionUtils.getStackTrace(_e));
				throw new PwtzDatabaseException();
			}
			totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
		}else{
			pageNo=1;
			if(list!=null&&list.size()>0){
				totalCount=list.size();
			}
		}
		paginationList.setCurrPage(pageNo);
		paginationList.setPageSize(pageSize);
		paginationList.setTotalPage(totalPage);
		paginationList.setTotalRecord(totalCount);
		paginationList.setRecords(list);
		return paginationList;
	}

	@Override
	public List<Zykzsxsbzp> findZykzsxsbzpsByDeviceid(String deviceid) throws PwtzDatabaseException {
	    List<Zykzsxsbzp> list=null;
        if(_logger.isInfoEnabled())
        {
            _logger.info("findZykzsxsbzpsByDeviceid:deviceid="+deviceid);
        }
        try{
            list=objMapper.selectByDeviceId(deviceid);
        }catch(DataAccessException _e){
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        if(list!=null)
        {
            _logger.debug("findZykzsxsbzpsByDeviceid:result list="+list.toString());
        }
        return list;
    }
    @Override
    public int selectMaxIndexNumber(String deviceid, String phototype) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("selectMaxNumber:|deviceid="+deviceid+"|phototype="+phototype);
        }
        int result=0;
        try{
            result=objMapper.selectMaxIndexNumber(deviceid, phototype);
            if(result==0){
                result=1;
            }
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
}
